package com.springboot.demo.controller;


import com.cx.item.common.par.PageParam;
import com.cx.item.common.par.check.CheckParTarget;
import com.cx.item.common.par.check.CheckParTargetInstance;
import com.cx.item.common.vo.ResultPageVo;
import com.github.pagehelper.PageInfo;
import com.springboot.demo.model.model.CustomerLoginInfo;
import com.springboot.demo.model.par.OrderPar;
import com.springboot.demo.model.vo.OrderVo;
import com.springboot.demo.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by Heavin on 2018/6/25.
 * Des
 */
@Api(description = "订单")
@Controller
@RequestMapping("/order")
@Slf4j
public class OrderController extends BaseController {

    @Autowired
    private OrderService orderService;

    @ApiOperation("查询所有")
    @RequestMapping(value = "/queryList", method = RequestMethod.GET)
    @ResponseBody
    public ResultPageVo<PageInfo<List<OrderVo>>> queryList(Integer payStatus, PageParam pageParam) {
        log.info("订单查询");

        CustomerLoginInfo currUserLoginInfo = getCurrUserLoginInfo();

        PageInfo<List<OrderVo>> bannerList = orderService.queryList(payStatus, currUserLoginInfo.getCustomerId(), pageParam);

        return ResultPageVo.success(bannerList);
    }

    @ApiOperation("添加")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public ResultPageVo add(OrderPar orderPar) {
        CheckParTarget instance = CheckParTargetInstance.getInstance(orderPar,
                "goodsId", "serviceLevel", "serviceBeginTime", "serviceEndTime"
                , "name", "phone", "detailedAddress");
        instance.checkParIsNullThrowError();
        if (orderPar.getPhone().length() != 11) {
            return ResultPageVo.failure("请输入有效的手机电话号码");
        }

        CustomerLoginInfo currUserLoginInfo = getCurrUserLoginInfo();
        orderPar.setCustomerId(currUserLoginInfo.getCustomerId());
        Integer bannerResult = orderService.create(orderPar);
        return ResultPageVo.success(bannerResult);


    }

   /* @ApiOperation("修改订单状态")
    @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
    @ResponseBody
    public ResultPageVo updateStatus(OrderParForStatus orderParForStatus) {
        CheckParTarget instance = CheckParTargetInstance.getInstance(orderParForStatus, "orderId", "payStatus");
        instance.checkParIsNullThrowError();
        int i = orderBGService.updateStatus(orderParForStatus);

        return ResultPageVo.success(i);
    }
*/

    @ApiOperation("获取")
    @RequestMapping(value = "/getById", method = RequestMethod.GET)
    @ResponseBody
    public ResultPageVo<OrderVo> getById(Integer id) {

        OrderVo orderVo = orderService.queryById(id);

        return ResultPageVo.success(orderVo);
    }
}